# config.json 文件操作

import json


# json 映射表
"""
jsonRouters = {
    "IP":"['Bind']['IP']",
    "Port":"['Bind']['Port']"
}
"""
# 基类
class BaseHandler():
    def __init__(self):
        super().__init__()


# 文件服务
class FileHandler(BaseHandler):
    # 读文件
    def readFile(self):
        with open('config/config.json', 'r') as f:
            fileJson = json.load(f)
            return fileJson
      

# Web服务
class WebHandler(BaseHandler):
    # 线程读取
    def __init__(self):
        self.threadNum = 1 # 初始化线程数为1

    def getThreadNum(self, fileJson):
        if (fileJson['web']['isUseThread'] == False):
            return self.threadNum
        
        return fileJson['web']['max_threadNum']
    

# 数据库服务
class DBHandler(BaseHandler):
    # 配置取值
    def getDBConfigs(self, fileJson):
        DBConfig = []
        DBConfig.append(fileJson['conn']['host'])
        DBConfig.append(fileJson['conn']['port'])
        # DBConfig.append(fileJson['conn']['user'])         THole
        DBConfig.append(fileJson['conn']['pwd'])    # 随机生成
        # DBConfig.append(fileJson['conn']['db'])           thole
        # DBConfig.append(fileJson['conn']['charset'])      utf-8
        return DBConfig

# 配置取值
class configs(BaseHandler):
    def __init__(self):
        super().__init__()

        file = FileHandler()
        web = WebHandler()
        db = DBHandler()


        fileJson = file.readFile()

        DBConfig = db.getDBConfigs(fileJson)


        self.ip = fileJson['bind']['ip']
        self.port = fileJson['bind']['port']

        self.threadNum = web.getThreadNum(fileJson)

        self.connHost = DBConfig[0]
        self.connPort = DBConfig[1]
        self.connPwd = DBConfig[2]

        print(1)



if __name__ == "__main__":
    c = configs()